<template>
	<view class="wrap">
		<u-form :model="model" :rules="rules" ref="uForm" :errorType="errorType">
			<u-form-item label="货品" prop="typeName">
				<u-input disabled="true" v-model="model.typeName" type="text"></u-input>
			</u-form-item>
			<u-form-item label="仓单" prop="initArea">
				<u-input disabled="true" v-model="model.initArea" type="text"></u-input>
			</u-form-item>
			<u-form-item label="单价" prop="price">
				<u-input placeholder="请输入货品单价" v-model="model.price" type="digit"></u-input>
			</u-form-item>
			<u-form-item label="单位" prop="inventoryPlace">
				<u-input disabled="true" v-model="model.inventoryPlace" type="text"></u-input>
			</u-form-item>
			<u-form-item v-show="model.payStatus==='0'||model.payStatus==='2'" label="应收" prop="type">
				{{model.payStatus==='0'?'应收':'已收'}}
				<u-switch v-model="isGet" slot="right" @change="switchChangeGet"></u-switch>
			</u-form-item>
			<u-form-item v-show="model.payStatus==='1'||model.payStatus==='3'" label="应付" prop="type">
				{{model.payStatus==='1'?'应付':'已付'}}
				<u-switch v-model="isPay" slot="right" @change="switchChangePay"></u-switch>
			</u-form-item>
		</u-form>
		<view class="record-btn">
			<u-button type="primary" shape="circle" size="default" @click="submit">记 录</u-button>
		</view>
	</view>
</template>

<script>
	import {
		updatePrice
	} from '../../api/updatelist.js'

	export default {
		data() {
			return {
				model: {},
				status: [{
						value: 0,
						label: '应收待收款'
					},
					{
						value: 1,
						label: '应付待付款'
					},
					{
						value: 2,
						label: '应收已收款'
					},
					{
						value: 3,
						label: '应付已付款'
					}
				],
				rules: {
					price: [{
						required: true,
						message: '请输入单价',
						trigger: 'blur',
					}]
				},
				errorType: ['message'],
				isPay: false,
				isGet: false
			};
		},
		onLoad(e) {
			this.model = JSON.parse(e.item)
			this.isPay = this.model.payStatus === '1' ? false : true
			this.isGet = this.model.payStatus === '0' ? false : true
		},
		methods: {
			switchChangeGet(status) {
				this.model.payStatus = this.isGet ? '2' : '0'
			},
			switchChangePay(status) {
				this.model.payStatus = this.isPay ? '3' : '1'
			},
			submit() {
				this.model.price = parseFloat(this.model.price)
				this.$refs.uForm.validate(valid => {
					if (valid) {
						updatePrice(this.model).then(res => {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							})
							if (res.msg === "修改成功") {
								uni.reLaunch({
									url: "./index"
								})
							}
						})

					}
				});
			}
		}
	};
</script>

<style scoped lang="scss">
	.wrap {
		padding: 30rpx;
	}

	.record-btn {
		padding-top: 50rpx;
	}
</style>
